//
//  SocketChannel.h
//  socket
//
//  Created by  user on 11-10-22.
//  Copyright 2011 GuangZhouXuanWu. All rights reserved.
//

#import <Foundation/Foundation.h>

#import "RequestChannel.h"

#define SENDRECVMAXTIMEOUT  9999

typedef NS_ENUM(NSUInteger,ESocketOperateResultType)
{
	ESocketOperateResultTypeSend=1,
	ESocketOperateResultTypeRecv=2,
	ESocketOperateResultTypeError=3,
	ESocketOperateResultTypeWriteTimeout=4,
	ESocketOperateResultTypeReadTimeout=5,
    ESocketOperateResultTypeFinish=6,
    ESocketOperateResultTypeConnectTimeout=7,
    ESocketOperateResultTypeConnect=8,
};

typedef NS_ENUM(NSUInteger,ECloseSocketOperateType)
{
	ECloseSocketOperateTypeNone=0,
	ECloseSocketOperateTypeRecv=1,
	ECloseSocketOperateTypeSend=2,
    ECloseSocketOperateTypeSendFinish=3,
    ECloseSocketOperateTypeRecvFinish=4
};

@class CMemoryStream;

@class CSocketChannel;

@protocol CSocketChannelDelegate<NSObject>

@optional

-(void)socketOperateResult:(CSocketChannel*)sc type:(ESocketOperateResultType)type result:(NSInteger)nResult;

-(void)socketChannel:(CSocketChannel*)sc willSendTotalBytes:(NSUInteger)bytes;

-(void)socketChannel:(CSocketChannel*)sc hadSendBytes:(NSUInteger)bytes;

@end

@interface CSocketChannel : CRequestChannel

@property(nonatomic,retain) NSString* IP;
@property(nonatomic,assign) NSUInteger port;

@property(nonatomic,retain) CMemoryStream* request;
@property(nonatomic,retain) CMemoryStream* response;

@property(nonatomic,assign) id<CSocketChannelDelegate> delegate;

@property(nonatomic,assign) NSTimeInterval sendTimeout;
@property(nonatomic,assign) NSTimeInterval recvTimeout;

-(id)initWithIP:(NSString*)szIP port:(NSInteger)nPort;

-(void)closeSocket;

-(BOOL)initSocketChannel;

/////////////

-(NSInteger)initLongConnectSocketChannel;

-(BOOL)beginRequest;

-(BOOL)endRequest;

@end

